var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;



layui.use(["excel"], function () {
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function(){
        laydate.render({
            elem: this
        });
    });

    layer.ready(function () {

        //获取所有供应商
        $.ajax({
            type : 'get',
            url:'customer2/getAllGys',
            dataType:"json",
            success : function (data) {
                if(data.status==200){
                    for (let i in data.data){
                        // $("#supplierCode").append("<option value=\"" + data.data[i].code + "\">" + data.data[i].name + "</option>");
                        $("#supplierCode").append("<option value=\"" + data.data[i].name + "\">" + data.data[i].name + "</option>");
                    }
                    form.render('select');
                }
            },
        });


    });
    form.render();//表单刷新
    let initData = [];
    table.render({
        elem: '#billing1Select'
        ,data:initData
        ,where:{
            sourceType:1
        }
        ,toolbar: '#billing1Toolbar'
        ,totalRow: true
        ,cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            , {field: 'purchaseOrderCode', title: '采购单号', sort: true, width: 200, align: 'center'}
            , {field: 'contractCode', title: '外销合同号', sort: true, width: 120, align: 'center'}
            , {field:'declarationInvoiceCode', title:'报关发票号',sort:true,width: 120, align: 'center'}
            , {field: 'productCode', title: '产品编码', sort: true, width: 120, align: 'center'}
            , {field: 'specificationCode', title: '规格编码', sort: true, width: 120, align: 'center'}
            , {field:'number', title:'开票数量',sort:true,width: 120, align: 'center', totalRow: true}
            , {field:'unitPrice', title:'开票单价',sort:true,width: 120, align: 'center', totalRow: true}
            , {field: 'subtotal', title: '开票金额小计', sort: true, width: 120, align: 'center', totalRow: true}
            , {field:'supplierName', title:'厂商名称',sort:true,width: 120, align: 'center'}
            , {field:'currencyName', title:'币种',sort:true,width: 120, align: 'center'}
            , {field:'createDateStr', title:'创建日期',sort:true,width: 120, align: 'center'}
            , {field:'billingDateStr', title:'发票收到日期',sort:true,width: 150, align: 'center'}
            , {field: 'goodsNum', title: '报关产品数量', sort: true, width: 120, align: 'center', totalRow: true}
            , {field: 'amountInvoiced', title: '需开票金额', sort: true, width: 120, align: 'center', totalRow: true}
            , {field: 'notAmountInvoiced', title: '未开票金额', sort: true, width: 120, align: 'center', totalRow: true}
            , {field: 'productType', title: '产品类型', sort: true, width: 120, align: 'center'}
        ]]
        ,id: 'maininformationtableid'
        ,height: $(document).height() - $('#billing1Select').offset().top - 75,
        //开启分页
        page: true,
        limits: [300,600,900],
    });


//搜索提交
    form.on('submit(submitform)', function(data){
        table.reload('maininformationtableid', {
            page: {
                limit:300,
                curr: 1
            },
            url:'billing/billingStatistics1ListByPage',
            where:  data.field
        }, 'data');
        return false;
    });

    form.on('submit(billingExcel)', function (data) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: data.field,
            url:'billing/billingStatistics1Export',
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 120,
                        'AJ': 120
                    }, 120);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '开票情况统计.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });

});

